package com.example.zhunongzonghe.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.zhunongzonghe.beans.ZnHouseEvaluateBean;
import com.example.zhunongzonghe.common.Result;
import com.example.zhunongzonghe.service.ZnHouseEvaluateService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.security.Principal;

@RestController
@RequestMapping("/ZnHouseEvaluate")
public class ZnHouseEvaluateController {
    @Resource
    private ZnHouseEvaluateService houseEvaluateService;


    /**
     * 用户新增评价
     * @param znHouseEvaluateBean
     * @param principal
     * @return
     */
    @PostMapping("/addEvaluate")
    public Result HouseEvaluate(@RequestBody ZnHouseEvaluateBean znHouseEvaluateBean, Principal principal){
        if (znHouseEvaluateBean.getScore()==null&&znHouseEvaluateBean.getContent()==null){
            return new Result(false,"数据异常",400);
        }
        houseEvaluateService.addHouseEvaluate(znHouseEvaluateBean,principal.getName());
        return new Result(true,"成功",200);
    }

    @PostMapping("/getEvaluate")
    public Result getEvaluate(@RequestBody ZnHouseEvaluateBean znHouseEvaluateBean, Principal principal){
        if (znHouseEvaluateBean.getReserveId()==null){
            return new Result(false,"数据异常",400);
        }
        ZnHouseEvaluateBean bean = houseEvaluateService.getEvaluate(znHouseEvaluateBean.getReserveId(),principal.getName());
        return new Result(true,"成功",200,bean);
    }
    /**
     * 用户编辑当前农家乐预定的评价
     * @param znHouseEvaluateBean
     * @param principal
     * @return
     */
    @PostMapping("/EditEvaluate")
    public Result EditEvaluate(@RequestBody ZnHouseEvaluateBean znHouseEvaluateBean, Principal principal){
        if (znHouseEvaluateBean.getScore()==null&&znHouseEvaluateBean.getContent()==null){
            return new Result(false,"数据异常",400);
        }
        houseEvaluateService.editHouseEvaluate(znHouseEvaluateBean,principal.getName());
        return new Result(true,"成功",200);
    }

    /**
     * 查询全部农家乐评价
     * @param houseId
     * @param createPage
     * @param pageSize
     * @return
     */
    @GetMapping("/getHouseEvaluateAll")
    public Result selectEvaAll(@RequestParam("houseId")Integer houseId,
                               @RequestParam("createPage")Integer createPage,
                               @RequestParam("pageSize")Integer pageSize){
        if (houseId==null) return new Result(false,"数据异常",400);
        Page<ZnHouseEvaluateBean> beanPage = houseEvaluateService.selectEvaAll(houseId,createPage,pageSize);
        return new Result(true,"成功",200,beanPage);
    }

    /**
     * 查询评分为4和5的农家乐评价
     * @param houseId
     * @param createPage
     * @param pageSize
     * @return
     */
    @GetMapping("/getHouseEvaluateFourAndFive")
    public Result selectFourAndFive(@RequestParam("houseId")Integer houseId,
                                    @RequestParam("createPage")Integer createPage,
                                    @RequestParam("pageSize")Integer pageSize){
        if (houseId==null) return new Result(false,"数据异常",400);
        Page<ZnHouseEvaluateBean> beanPage = houseEvaluateService.selectFourAndFive(houseId,createPage,pageSize);
        return new Result(true,"成功",200,beanPage);
    }

    /**
     * 查询评分为3的农家乐评价
     * @param houseId
     * @param createPage
     * @param pageSize
     * @return
     */
    @GetMapping("/getHouseEvaluateThree")
    public Result selectEvaThree(@RequestParam("houseId")Integer houseId,
                                 @RequestParam("createPage")Integer createPage,
                                 @RequestParam("pageSize")Integer pageSize){
        if (houseId==null) return new Result(false,"数据异常",400);
        Page<ZnHouseEvaluateBean> beanPage = houseEvaluateService.selectThree(houseId,createPage,pageSize);
        return new Result(true,"成功",200,beanPage);
    }

    /**
     * 查询评分为1和2的农家乐评价
     * @param houseId
     * @param createPage
     * @param pageSize
     * @return
     */
    @GetMapping("/getHouseEvaluateOneAndTow")
    public Result selectOneAndTow(@RequestParam("houseId")Integer houseId,
                                  @RequestParam("createPage")Integer createPage,
                                  @RequestParam("pageSize")Integer pageSize){
        if (houseId==null) return new Result(false,"数据异常",400);
        Page<ZnHouseEvaluateBean> beanPage = houseEvaluateService.selectOneAndTow(houseId,createPage,pageSize);
        return new Result(true,"成功",200,beanPage);
    }

    /**
     * 后台查询全部农家乐评价
     * @param currentPage
     * @param pageSize
     * @param searchInfo
     * @return
     */
    @GetMapping("/getMagEvaluateAll")
    public Result getMagEvaluateAll (@RequestParam("currentPage")Integer currentPage,
                                     @RequestParam("pageSize")Integer pageSize,
                                     @RequestParam("searchInfo")Object searchInfo){
        Page<ZnHouseEvaluateBean> beanPage = houseEvaluateService.selectEvaluateAll(currentPage,pageSize,searchInfo);
        if (currentPage > beanPage.getPages()){
            beanPage=houseEvaluateService.selectEvaluateAll((int) beanPage.getPages(),pageSize,searchInfo);
        }
        return new Result(true,"成功",200,beanPage);
    }

    /**
     * 后台删除评价
     * @param id
     * @return
     */
    @DeleteMapping("/DeleteHouseEvaluate/{id}")
    public Result deleteHouseEvaluate(@PathVariable Integer id){
        if (id==null) return new Result(false,"失败",400);
        houseEvaluateService.removeById(id);
        return new Result(true,"成功",200);
    }
}
